package fy.form.groovy;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.bo.V3xOrgPost;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.v3x.services.V3XLocator;

import fy.config.FyConfig;
import fy.util.DBUtil;

public class GroovyFuncFYR {
	private static final Log logger = LogFactory.getLog(GroovyFuncFYR.class);

	public static int judgeDirector(Object object) throws Exception {
		Object[] array = (Object[]) object;
		System.out.println(array[0].toString().trim()+"~~~~~~~~~~~~~"+array[1].toString().trim()+"~~~~~~~~~~~~~~~~~~~"+array[2].toString().trim());
		String deptID = array[0].toString().trim();
		String unitID = array[1].toString().trim();
		String memberID = array[2].toString().trim();
		
		String memberIDQuery = queryMemberID(deptID, unitID);
		
		logger.info("deptID" + deptID+unitID+memberID+memberIDQuery);
		System.out.println("1****************"+deptID+unitID+memberID+memberIDQuery);
		if (!memberIDQuery.equals("") || memberIDQuery != null) {
			if (!memberID.equals(memberIDQuery)) {
				return 1;
			}
		}
		return 0;
	}

	// 查询人员ID是否存在
	public static String queryMemberID(String deptID, String unitID) throws Exception {
		System.out.println("2****************"+deptID+unitID);
		Connection conn = null;
		PreparedStatement pst = null;
		String memberID = null;
		boolean res = false;
		ResultSet rs = null;
		conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
		try {
			pst = conn.prepareStatement(
					"SELECT o.SOURCE_ID FROM ORG_RELATIONSHIP o,(SELECT id FROM ORG_POST WHERE NAME='Approval Director' AND is_enable=1) p WHERE o.OBJECTIVE1_ID=p.id AND o.OBJECTIVE5_ID='Second' AND o.\\\"TYPE\\\"='Member_Post'  AND o.OBJECTIVE0_ID=? AND o.ORG_ACCOUNT_ID=?");
			pst.setString(1,deptID);
			pst.setString(2, unitID);
			rs = pst.executeQuery();
			while (rs.next()) {
				memberID = rs.getString(1);
				System.out.println("3****************"+memberID);
			}
		} finally {
			DBUtil.close(rs);
			DBUtil.close(pst);
		}
		return memberID;
	}

}